home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / umoria / signals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  5.3 KB  |  283 lines

  1. #include <curses.h>
  2. #include <signal.h>
  3.  
  4. #include "constants.h"
  5. #include "config.h"
  6. #include "types.h"
  7. #include "externs.h"
  8.  
  9. #ifdef USG
  10. #include <string.h>
  11. #else
  12. #include <strings.h>
  13. #endif
  14.  
  15. extern int total_winner;
  16. extern int moria_flag;
  17. extern int search_flag;
  18.  
  19. #ifdef USG
  20. /* no local special characters */
  21. #else
  22. extern struct ltchars save_special_chars;
  23. #endif
  24.  
  25. int error_sig, error_code;
  26.  
  27. /* This signal package was brought to you by           -JEW-  */
  28.  
  29. /* on some systems, signal and suspend_handler must be declared as 'void'
  30.    instead of 'int', this varies even among different OS releases, so there
  31.    is no good way to solve the problem with ifdefs */
  32. /* int (*signal())(); */
  33. int signal_save_core();
  34. int signal_save_no_core();
  35. int signal_ask_quit();
  36.  
  37. #ifdef USG
  38. /* no suspend signal */
  39. #else
  40. int (*suspend_handler)();
  41. #endif
  42.  
  43. /* only allow one signal handler to execute, otherwise can get multiple save
  44.        files by sending multiple signals */
  45. static int caught_signal = 0;
  46.  
  47. init_signals()
  48. {
  49.   (void) signal(SIGHUP, signal_save_no_core);
  50.   (void) signal(SIGINT, signal_ask_quit);
  51.   (void) signal(SIGQUIT, signal_save_core);
  52.   (void) signal(SIGILL, signal_save_core);
  53.   (void) signal(SIGTRAP, signal_save_core);
  54.   (void) signal(SIGIOT, signal_save_core);
  55.   (void) signal(SIGEMT, signal_save_core);
  56.   (void) signal(SIGFPE, signal_save_core);
  57.   (void) signal(SIGKILL, signal_save_core);
  58.   (void) signal(SIGBUS, signal_save_core);
  59.   (void) signal(SIGSEGV, signal_save_core);
  60.   (void) signal(SIGSYS, signal_save_core);
  61.   (void) signal(SIGTERM, signal_save_core);
  62. }
  63.  
  64. /*ARGSUSED*/
  65. #ifdef USG
  66. signal_save_core(sig)
  67. int sig;
  68. {
  69.   if (caught_signal)
  70.     return;
  71.   caught_signal = 1;
  72.   panic_save = 1;
  73.  
  74.   error_sig = sig;
  75.   error_code = 0;
  76.   if (!character_saved)
  77.     {
  78.       prt("OH NO!!!!!!!!!!  Attempting panic save.", 23, 0);
  79.       save_char(FALSE, FALSE);
  80.     }
  81.   (void) signal(SIGQUIT, SIG_DFL);
  82.   /* restore terminal settings */
  83. #ifndef BUGGY_CURSES
  84.   nl();
  85. #endif
  86. #if defined(ultrix)
  87.   nocrmode();
  88. #else
  89.   nocbreak();
  90. #endif
  91.   echo();
  92.   resetterm();
  93.   /* restore the saved values of the local special chars */
  94.   /* no local special characters */
  95.   /* allow QUIT signal */
  96.   /* nothing needs to be done here */
  97.   (void) kill(getpid(), 3);
  98.   exit_game();
  99. }
  100. #else
  101. signal_save_core(sig, code, scp)
  102. int sig, code;
  103. struct sigcontext *scp;
  104. {
  105.   if (caught_signal)
  106.     return;
  107.   caught_signal = 1;
  108.   panic_save = 1;
  109.  
  110.   error_sig = sig;
  111.   error_code = code;
  112.   if (!character_saved)
  113.     {
  114.       prt("OH NO!!!!!!!!!!  Attempting panic save.", 23, 0);
  115.       save_char(FALSE, FALSE);
  116.     }
  117.   (void) signal(SIGQUIT, SIG_DFL);
  118.   /* restore terminal settings */
  119. #ifndef BUGGY_CURSES
  120.   nl();
  121. #endif
  122. #if defined(ultrix)
  123.   nocrmode();
  124. #else
  125.   nocbreak();
  126. #endif
  127.   echo();
  128.   /* restore the saved values of the local special chars */
  129.   (void) ioctl(0, TIOCSLTC, (char *)&save_special_chars);
  130.   /* allow QUIT signal */
  131.   (void) sigsetmask(0);
  132.   (void) kill(getpid(), 3);
  133.   exit_game();
  134. }
  135. #endif
  136.  
  137. /*ARGSUSED*/
  138. #ifdef USG
  139. signal_save_no_core(sig)
  140. int sig;
  141. {
  142.   if (caught_signal)
  143.     return;
  144.   caught_signal = 1;
  145.   panic_save = 1;
  146.  
  147.   error_sig = sig;
  148.   error_code = 0;
  149.   if (!character_saved)
  150.     save_char(FALSE, TRUE);
  151.   exit_game();
  152. }
  153. #else
  154. signal_save_no_core(sig, code, scp)
  155. int sig, code;
  156. struct sigcontext *scp;
  157. {
  158.   if (caught_signal)
  159.     return;
  160.   caught_signal = 1;
  161.   panic_save = 1;
  162.  
  163.   error_sig = sig;
  164.   error_code = code;
  165.   if (!character_saved)
  166.     save_char(FALSE, TRUE);
  167.   exit_game();
  168. }
  169. #endif
  170.  
  171. /*ARGSUSED*/
  172. #ifdef USG
  173. signal_ask_quit(sig)
  174. int sig;
  175. {
  176.   char command;
  177.  
  178.   /* reset signal handler */
  179.   (void) signal(sig, signal_ask_quit);
  180.   find_flag = FALSE;
  181.   if (search_flag)
  182.     search_off();
  183.   if (py.flags.rest > 0)
  184.     {
  185.       rest_off();
  186.       return;
  187.     }
  188.   if (get_com("Do you really want to quit?", &command))
  189.     switch(command)
  190.       {
  191.       case 'y': case 'Y':
  192.         if (character_generated)
  193.       {
  194.         (void) strcpy(died_from, "Quitting.");
  195.         upon_death();
  196.       }
  197.         else
  198.           exit_game();
  199.     break;
  200.       }
  201.   erase_line(MSG_LINE, 0);
  202.   /* in case control-c typed during msg_print */
  203.   if (wait_for_more)
  204.     {
  205.       put_buffer(" -more-", MSG_LINE, 0);
  206.       put_qio();
  207.     }
  208. }
  209. #else
  210. signal_ask_quit(sig, code, scp)
  211. int sig, code;
  212. struct sigcontext *scp;
  213. {
  214.   char command;
  215.  
  216.   /* no need to reset signal handler */
  217.   find_flag = FALSE;
  218.   if (search_flag)
  219.     search_off();
  220.   if (py.flags.rest > 0)
  221.     {
  222.       rest_off();
  223.       return;
  224.     }
  225.   if (get_com("Do you really want to quit?", &command))
  226.     switch(command)
  227.       {
  228.       case 'y': case 'Y':
  229.     if (character_generated)
  230.       {
  231.         (void) strcpy(died_from, "Quitting.");
  232.         upon_death();
  233.       }
  234.     else
  235.       exit_game();
  236.     break;
  237.       }
  238.   erase_line(MSG_LINE, 0);
  239.   /* in case control-c typed during msg_print */
  240.   if (wait_for_more)
  241.     {
  242.       put_buffer(" -more-", MSG_LINE, 0);
  243.       put_qio();
  244.     }
  245. }
  246. #endif
  247.  
  248. no_controlz()
  249. {
  250. #ifdef USG
  251.   /* no suspend signal */
  252. #else
  253.   suspend_handler = signal(SIGTSTP, SIG_IGN);
  254. #endif
  255. }
  256.  
  257. controlz()
  258. {
  259. #ifdef USG
  260.   /* no suspend signal */
  261. #else
  262.   (void) signal(SIGTSTP, suspend_handler);
  263. #endif
  264. }
  265.  
  266. ignore_signals()
  267. {
  268.   (void) signal(SIGINT, SIG_IGN);
  269.   (void) signal(SIGQUIT, SIG_IGN);
  270. }
  271.  
  272. default_signals()
  273. {
  274.   (void) signal(SIGINT, SIG_DFL);
  275.   (void) signal(SIGQUIT, SIG_DFL);
  276. }
  277.  
  278. restore_signals()
  279. {
  280.   (void) signal(SIGINT, signal_ask_quit);
  281.   (void) signal(SIGQUIT, signal_save_core);
  282. }
  283.